var listUrl = rootPath+'/manage/examPaper';//答卷校验列表
var uploadUrl = rootPath+'/manage/examPaper/upload/uploadExamPaperUI';//上传地址
var checkTempUrl = rootPath+'/manage/examPaper/check/checkTemplateExists';//校验模板是否存在
var checkFailPaperUrl = rootPath+'/manage/examPaper/check/checkFailPaperExists';//查询校验失败的答卷
var clearPaperUrl = rootPath+'/manage/examPaper/check/clearPaper/exec';//清空答卷
var cropUrl = rootPath+'/manage/examPaper/crop/cropPaper/exec';//执行答卷裁剪

var paperCheckUrl = rootPath + '/manage/examPaper/check/getPaperCheckSession';//答卷校验进度
var paperCropUrl = rootPath + '/manage/examPaper/crop/getPaperCropSession';//答卷裁剪进度
var paperCorrectionUrl = rootPath + '/manage/examPaper/paperCorrectionUI';//答卷校正
var paperAlreadyUploadUrl = rootPath + '/manage/examPaper/papers/paperAlreadyUploadUI';//查询已上传的考生答卷
var paperSurplusStudentUrl = rootPath + '/manage/examPaper/paperSurplusStudentUI';//查询答卷未入系统考生

var destory_value1 =0;
var destory_value2 =0;
var progressbar = $("#progressbar"),progressLabel = $(".progress-label");
var progressbar2 = $("#progressbar2"),progressLabel2 = $(".progress-label2");

$(function(){
	queryGradeByBatch();
	$('#query_batch').change(function(){
		queryGradeByBatch();
	});
	$('#query_grade').change(function(){
		queryCourseByGrade();
	});
	
	$('.btn-upload').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		paperUpload(batchId,gradeNo,courseNo);
	});
	$('.btn-check').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		checkProgress(1,batchId,gradeNo,courseNo);
	});
	$('.btn-kh-check').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		paperCorrection(batchId,gradeNo,courseNo);
	});
	$('.btn-detail').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		queryPaperAlreadyUpload(batchId,gradeNo,courseNo);
	});
	$('.btn-clear').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		clearPaper(batchId,gradeNo,courseNo);
	});
	$('.btn-surplus-ks').click(function(){
		var batchId = $(this).parents('tr').attr('data-batchid');
		var gradeNo = $(this).parents('tr').attr('data-gradeno');
		var courseNo = $(this).parents('tr').attr('data-courseno');
		surplusStudent(batchId,gradeNo,courseNo);
	});
	initJybar();
	initCutbar();
});

//根据批次查询年级
var queryGradeByBatch = function(){
	common_ajax.ajax(rootPath+'/manage/courseTemplate/queryGradeByBatch', {batchId : $('#query_batch option:selected').val()}, 'json', function(data){
		if(data.list){
			var html = '<option value="">---请选择---</option>';
			$(data.list).each(function(index, item){
				html += '<option value="'+item.gradeNo+'"';
				if(item.gradeNo==query_grade){
					html += ' selected="selected"';
				}
				html += '>'+item.gradeName+'</option>';
			});
			$('#query_grade').html(html);
			queryCourseByGrade();
		}
	});
}
//根据年级查询课程
var queryCourseByGrade = function(){
	var batchId = $('#query_batch option:selected').val();
	var gradeNo = $('#query_grade option:selected').val();
	if(query_grade != null && query_course != null){
		batchId = query_grade;
		gradeNo = query_course;
	}
	common_ajax.ajax(rootPath+'/manage/courseTemplate/queryCourseByGrade', {batchId : batchId,gradeNo : gradeNo}, 'json', function(data){
		if(data.list){
			var html = '<option value="">---请选择---</option>';
			$(data.list).each(function(index, item){
				html += '<option value="'+item.courseNo+'"';
				if(item.courseNo==query_course){
					html += ' selected="selected"';
				}
				html += '>'+item.courseName+'</option>';
			});
			$('#query_course').html(html);
		}
	});
}

//答卷上传
function paperUpload(batchId,gradeNo,courseNo){
	var pageii = layer.open({
		title : '科目答卷上传',
		type : 2,
		area : [ '600px', '200px' ],
		content : uploadUrl + '?batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo
	});
}

//查询已上传的考生答卷
function queryPaperAlreadyUpload(batchId,gradeNo,courseNo){
	var index_paper = layer.open({
		title : '已上传答卷列表',
		type : 2,
		content : paperAlreadyUploadUrl + '?batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo
	});
	layer.full(index_paper);
}

function saveCallBack(){
	refreshBack();
}

//刷新事件
function refreshBack(){
	location.href = listUrl;
}

//科目模板查询校验
function checkTemplateExists(batchId,gradeNo,courseNo){
	common_ajax.ajax(checkTempUrl, {
		batchId : batchId,
		gradeNo : gradeNo,
		courseNo : courseNo
	}, 'json');
}

//科目答卷切割
function kmdjCut(batchId,gradeNo,courseNo){
	common_ajax.ajax(cropUrl, {
		batchId : batchId,
		gradeNo : gradeNo,
		courseNo : courseNo
	}, 'json');
}

//查询科目答卷校验失败的信息
function paperCorrection(batchId,gradeNo,courseNo){
	common_ajax.ajax(checkFailPaperUrl, {
		batchId : batchId,
		gradeNo : gradeNo,
		courseNo : courseNo
	}, 'json', function(data){
		var resultCode = data.resultCode;
		if(resultCode=='000000'){
			//跳转到答卷校正页面
			var index = layer.open({
				title : '科目答卷校正',
				type : 2,
				area : [ '800px', '600px' ],
				content : paperCorrectionUrl + '?batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo
			});
			layer.full(index);
		}else if(resultCode =='000007'){
			layer.alert('科目模板未设置考生编号区域！',{icon:5});
		}else if(resultCode =='000009'){
			layer.alert('科目模板未设置缺考区域！！',{icon:5});
		}else{
			layer.msg('未查询到数据！');
		}
	});
}

//清空上传答卷信息
function clearPaper(batchId,gradeNo,courseNo){
	layer.confirm('您确定要清空该科目已上传的答卷吗？', function(index) {
		common_ajax.ajax(clearPaperUrl, {
			batchId : batchId,
			gradeNo : gradeNo,
			courseNo : courseNo
		}, 'json', function(data){
			if(data.resultCode=='000000'){
				layer.msg('操作成功！');
			}else{
				layer.alert('操作失败！',{icon: 5});
			}
		});
	});
}

//查询答卷未入系统考生
function surplusStudent(batchId,gradeNo,courseNo){
	layer.open({
		title : '查询未上传答卷考生',
		type : 2,
		area : [ '800px', '600px' ],
		content : paperSurplusStudentUrl + '?batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo
	});
}
/**************进度条部分****************/
//打开表单页面
function checkProgress(type,batchId,gradeNo,courseNo){
	if(type==1){
		openProgressDialog(type,'dialog_djjy',"答卷校验切割进度条",batchId,gradeNo,courseNo);
	}else{
		openProgressDialog(type,'dialog_djcut',"答卷切割进度条",batchId,gradeNo,courseNo);
	}
}

function openProgressDialog(type,win,title,batchId,gradeNo,courseNo){
	$.fx.speeds._default = 1000;//页面加载事件，数值越大加载越慢
	$('#'+win).dialog({
		title:title,
		autoOpen: false,
		show: "blind",
		hide: "slow",
		height: 150,
		width: 800,
		modal: true,
		draggable: false,
		closeOnEscape: false
	});
	
	//点击编辑按钮激发事件
	$('#'+win).dialog("open");
	if(type==1){
		destory_value1 = 0;
		//initJybar();
		setTimeout(progress, 1000);
		checkTemplateExists(batchId,gradeNo,courseNo);
	}else{
		setTimeout(progress2,1000);
		kmdjCut(batchId,gradeNo,courseNo);
	}
}

function initJybar(){
	progressbar.progressbar({
		value: false,
		change: function() {
	  		progressLabel.text(progressbar.progressbar("value") + "%");
		},
		complete: function() {
	  		progressLabel.text( "校验完成!" );
		}
	});
}
function initCutbar(){
	progressbar2.progressbar({
		value: false,
		change: function() {
	  		progressLabel2.text(progressbar2.progressbar("value") + "%");
		},
		complete: function() {
	  		progressLabel2.text( "答卷切割完成!" );
		}
	});
}

var progress = function() {
	//获取处理进度数据信息
	common_ajax.ajax(paperCheckUrl, {}, 'json', function(data_){
		var data = eval(data_.data);
		var bl = data.bl;
		if(bl=='0' || bl==''){
			$('#progress-show-text').html("正在执行校验，请勿关闭或刷新页面......");
		}else{
			var djzs = data.djzs;
			var clzs = data.clzs;
			if(clzs > djzs){
				clzs = djzs;
			}
			$('#progress-show-text').html("当前进度 "+clzs+"/"+djzs);
		}
		
		if(destory_value1==0){
			progressbar.progressbar("value", bl);
		}else{
			initJybar();
		}
		
		var resultCode = data_.resultCode;//状态值：0-模板没查到，1-操作成功，2-模板没查到,3-考生未查到,4-正面答卷未查到 ,9-成功
		if (bl <100 && (resultCode=='' || resultCode==null || typeof(resultCode) == "undefined")) {
			setTimeout(progress, 3000);
    	}
		
		if(resultCode!='' && resultCode!=null && typeof(resultCode) != "undefined"){
    		checkResultShow(resultCode,data);
    	}
	});
}

var progress2 = function() {
	//获取处理进度数据信息
	common_ajax.ajax(paperCropUrl, {}, 'json', function(data_){
		if(data_){
			var data = eval(data_.data);
			var bl = data.bl2;
			if(bl=='0' || bl==''){
				$('#progress-show-text').html("正在执行切割，请勿关闭或刷新页面......");
			}else{
				var djzs = data.djzs2;
				var clzs = data.clzs2;
				if(clzs > djzs){
					clzs = djzs;
				}
				$('#progress-show-text2').html("当前进度 "+clzs+"/"+djzs);
			}
				
			if(destory_value1==0){
				progressbar2.progressbar("value", bl);
			}else{
				initCutbar();
			}
	    	
			var resultCode = data_.resultCode;
	    	if (bl <100 && (resultCode=='' || resultCode==null || typeof(resultCode) == "undefined")) {
	      		setTimeout(progress2, 2000);
	    	}
			if(resultCode!='' && resultCode!=null && typeof(resultCode) != "undefined"){
				cropResultShow(resultCode,data);
			}
		}
	});
}

//答卷校验结果展示
function checkResultShow(resultCode,data){
	//状态值：0-模板没查到，1-操作成功，2-模板没查到,3-考生未查到,4-正面答卷未查到 ,9-成功
	destory_value1 = 1;
	$('#dialog_djjy').dialog("close");
	if(resultCode=='000000' || resultCode=='000004'){//★☆◆◇■
		var str='<span style="line-height: 25px;display: inline-block;">';
		var strchar='★ ';
		var message=str+'本次校验答卷总数【'+data.allCount+'】份</span><br>'
			+str+strchar+'成功数【'+data.succCount+'】份</span><br>'
			+str+strchar+'识别失败数【'+data.sbsbCount+'】份（包含正反面），考生比对失败数【'+data.bdsb+'】份（包含正反面）</span><br>'
			+str+strchar+'缺考数【'+data.qkCount+'】人</span>';
		layer.alert(message);
	}else if(resultCode=='000001'){
		layer.alert('对不起，未查询到设置的科目模板，请联系管理员检查模板设置！',{icon: 5});
		return false;
	}else if(resultCode=='000002'){
		layer.alert('对不起，未查询到考生数据！',{icon: 5});
		return false;
	}else if(resultCode=='000003'){
		layer.alert('对不起，未查询到答卷数据！',{icon: 5});
		return false;
	}else if(resultCode=='000005'){
		layer.alert('对不起，答卷模板未设置正面特征点区域！',{icon: 5});
		return false;
	}else if(resultCode=='000006'){
		layer.alert('对不起，答卷模板未设置反面特征点区域！',{icon: 5});
		return false;
	}else if(resultCode=='000007'){
		layer.alert('对不起，答卷模板未设置考生编号区域！',{icon: 5});
		return false;
	}else if(resultCode=='000008'){
		layer.alert('对不起，答卷模板未设置考生编号采集点或设置不正确！',{icon: 5});
		return false;
	}else if(resultCode=='000009'){
		layer.alert('对不起，答卷模板未设置缺考区域！',{icon: 5});
		return false;
	}
}

//答卷切割结果展示
function cropResultShow(resultCode,data){
	$("#progressbar2").progressbar("destroy");
	$('#dialog_djcut').dialog("close");
	$('#dialog_djcut').empty();
	destory_value2 = 1;
	if(resultCode=='000000'){
		var str='<span style="line-height: 25px;display: inline-block;">';
		var strchar='★ ';
		var message='答卷切割完成，记录总数【'+data.count+'】<br>'
			+str+strchar+'成功数【'+data.succCount+'】<br>'
			+str+strchar+'失败数【'+data.failCount+'】';
		layer.alert(message);
	}else{
		if(data.count==0){
			layer.alert('没有查询到答卷！',{icon: 5});
		}else{
			layer.alert('答卷切割失败，请联系系统管理员！',{icon: 5});
		}
	}
}